﻿#include "AlgorithmBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

AlgorithmBlock::AlgorithmBlock(QString &name, QRect &position, double zoom, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
}

AlgorithmBlock::~AlgorithmBlock()
{

}

void AlgorithmBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = m_aScale * zoom();
    double b = m_bScale * a;
    double bottom = y + height;
    double lw = 1.0 * zoom();

//    qDebug()<<"Algorithm->branchitems:"<<items.size();
    /* 算法 */
    QBlock *body = getBranchItem().at(0);
    Q_ASSERT_X(body != nullptr, "QBlock::paint()" ,"item(0) == 0. i.e. body of algorithm is nul.");

    SetNewPen(2)
    QBrush brush = canvas->brush();
    canvas->setBrush(m_backgroundColor);
    QRectF oval(hcenter - b/2, lw, b, a/2);
    canvas->drawRoundedRect(oval, a/4, a/4);
    canvas->setBrush(brush);
    canvas->drawLine(QLineF(hcenter, y + a/2+lw, hcenter, body->m_dY+0.5));
    canvas->drawLine(QLineF(hcenter, body->m_dY + body->m_dHeight-0.5, hcenter, bottom - a/2-lw));
    drawBottomArrow(canvas, QPointF(hcenter, bottom - a/2 - lw),
                                QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));

    UseOldPen

    canvas->drawText(oval, Qt::TextSingleLine | Qt::AlignHCenter | Qt::AlignVCenter, QObject::tr("BEGIN"));

    oval = QRectF(hcenter - b/2, bottom - a/2 - lw, b, a/2);

    SetNewPen(2)
    canvas->setBrush(m_backgroundColor);
    canvas->drawRoundedRect(oval, a/4, a/4);
    canvas->setBrush(brush);

    UseOldPen
    canvas->drawText(oval, Qt::TextSingleLine | Qt::AlignHCenter | Qt::AlignVCenter, QObject::tr("END"));

}

QString AlgorithmBlock::getBlockDom()
{
    QString xmlStr = QString("<algorithm><branch><assign dest=\"x\" src=\"0\" ID=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\"/></branch></algorithm>")
            .arg(1).arg(2).arg(3).arg(4);
    return  xmlStr;
}
}
}
